package com.xxg.fileshand.activity

import android.os.Bundle
import android.widget.Toast
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.EncryptUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.lxj.xpopup.XPopup
import com.xxg.fileshand.BuildConfig
import com.xxg.fileshand.app.App
import com.xxg.fileshand.app.SetInfo
import com.xxg.fileshand.base.BaseVMActivity
import com.xxg.fileshand.base.NoViewModel
import com.xxg.fileshand.base.ext.clickWithTrigger
import com.xxg.fileshand.base.ext.dataConvert
import com.xxg.fileshand.base.ext.getApiService
import com.xxg.fileshand.base.launch
import com.xxg.fileshand.config.Config
import com.xxg.fileshand.constant.Constant
import com.xxg.fileshand.databinding.ActivityLoginBinding
import com.xxg.fileshand.greendao.GreenDaoManager
import com.xxg.fileshand.net.NetWorkException
import com.xxg.fileshand.net.RetrofitManager
import java.util.Locale

class LoginActivity : BaseVMActivity<ActivityLoginBinding, NoViewModel>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        if (BuildConfig.DEBUG) {
//            LogUtils.e("时间戳：" + TimeUtils.getNowMills())
//            if (TimeUtils.getNowMills() > 1698332166000) {//1693725436000
//                LogUtils.e("Debug版本已过期")
//                Toast.makeText(this, "Debug版本已过期", Toast.LENGTH_LONG).show()
//                AppUtils.exitApp()
//            }
//        }
        if (SPUtils.getInstance().getString(Constant.BASE_URL).isNullOrEmpty()) {
            SPUtils.getInstance().put(Constant.BASE_URL, "http://43.138.126.105:8031/")
        }
        Config.baseUrl =
            SPUtils.getInstance().getString(Constant.BASE_URL) + "pda/commService/"

        PermissionUtils.permission(
            PermissionConstants.STORAGE,
        ).callback(object : PermissionUtils.SimpleCallback {
            override fun onGranted() {
            }

            override fun onDenied() {
                toast("请授予相关权限")
            }
        }).request()

        mBinding.etAccount.setText(SPUtils.getInstance().getString(Constant.LOGIN_ACCOUNT))
        mBinding.etPassword.setText(SPUtils.getInstance().getString(Constant.LOGIN_PASSWORD))

        mBinding.tvLogin.clickWithTrigger {
            if (Config.baseUrl.isEmpty()) {
                toast("请先设置服务器地址")
                return@clickWithTrigger
            }
            val account = mBinding.etAccount.text.toString()
            val password = mBinding.etPassword.text.toString()
            if (account.isEmpty() || password.isEmpty()) {
                toast("请输入账号密码")
                return@clickWithTrigger
            }
            if (SetInfo.isOffline()) {
                GreenDaoManager.findUserInfo(
                    account,
                    EncryptUtils.encryptMD5ToString(password).lowercase(Locale.getDefault())
                )?.let {
                    SPUtils.getInstance().put(Constant.LOGIN_ACCOUNT, account)
                    SPUtils.getInstance().put(Constant.LOGIN_PASSWORD, password)
                    App.userName = account
                    startActivity(HomeActivity::class.java)
                    finish()
                } ?: toast("账号密码错误")
            } else {
                launch({
                    val loginRp = getApiService().login(
                        mapOf(
                            Pair("user", account), Pair("pwd", password)
                        )
                    ).dataConvert()
                    SPUtils.getInstance().put(Constant.LOGIN_ACCOUNT, account)
                    SPUtils.getInstance().put(Constant.LOGIN_PASSWORD, password)
                    App.token = loginRp.token
                    App.userName = loginRp.userName
                    startActivity(HomeActivity::class.java)
                }, {
                    if (it is NetWorkException) {
                        toast(it.message)
                    }
                })
            }

        }
        mBinding.tvSetServer.clickWithTrigger {
            XPopup.Builder(this)
                .asInputConfirm(
                    "设置服务器IP地址",
                    null,
                    SPUtils.getInstance().getString(Constant.BASE_URL),
                    "x.x.x.x"
                ) {
                    if (it.isEmpty()) {
                        return@asInputConfirm
                    }
                    RetrofitManager.mRetrofitManager = null
                    SPUtils.getInstance().put(Constant.BASE_URL, it)
                    Config.baseUrl = it + "pda/commService/"
                }.show()
        }
    }
}